package com.zhaopin.gitlab;

import com.zhaopin.utils.FileUtil;
import com.zhaopin.utils.GitUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

@Slf4j
public class JustCheckExp {


    //职位推荐名称
    static String projectName = "project";
    //职位搜索名称
//    static String projectName = "position";
    static String outputDir = projectName.equals("project") ? "dir/gitlab/recommend" : "dir/gitlab/position";
    static String RPOJECT_NAME_FILE = "dir/gitlab/"+projectName+".txt";
    static String gitFile = "/Users/zhaopin/IdeaProjects/"+projectName+"/%s";
    static String inPath = "/Users/zhaopin/IdeaProjects/"+projectName+"/%s/project/src/main/java";

    //要替换的原值
//    static String old = "\"similarJobLevelForC\"\n" +
//            "\"similarJobLevelForCsea\"\n" +
//            "\"similarJobLevelForCV2\"\n" +
//            "\"similarJobLevelForCV3\"\n" +
//            "\"similarJobLevelForCV4\"\n" +
//            "\"similarJobLevelForCV_Exp\"\n" +
//            "\"similarJobLevelForCV_Exp1\"\n" +
//            "\"similarJobLevelForCV_Exp2\"\n" +
//            "\"similarJobLevelForCV_Exp3\"\n" +
//            "\"similarJobLevelForCV_Exp4\"";


//    static String old = "SOU_CONTENT\t\n" +
//            "\t\n" +
//            "SOU_POSITION_LARGETYPE\t\n" +
//            "\t\n" +
//            "SOU_POSITION_LARGETYPE_NEW\t\n" +
//            "\t\n" +
//            "SOU_POSITION_NAME\t\n" +
//            "\t\n" +
//            "SOU_POSITION_NAME_QUERY\t\n" +
//            "\t\n" +
//            "SOU_POSITION_URL\t\n" +
//            "\t\n" +
//            "JD_TITLE_SIMHASH\t\n" +
//            "\t\n" +
//            "SOU_POSITION_SMALLTYPE\t\n" +
//            "\t\n" +
//            "SOU_POSITION_SMALLTYPE_NEW\t\n"+
//            "\t\n" +
//            "JD_SKILL_TERMS_WEIGHT\t\n" +
//            "\t\n" +
//            "JD_STD_SKILL\t\n" +
//            "\t\n" +
//            "JD_STD_SKILL_CODE\t\n" +
//            "\t\n" +
//            "JD_UGC_SKILL\t\n" +
//            "\t\n" +
//            "JD_PROFE_SKILL_TAG\t\n" +
//            "\t\n" +
//            "JD_TITLE_TERMS_WEIGHT\t\n" +
//            "\t\n" +
//            "JD_TITLE_TERMS_WEIGHT_NEW\t\n" +
//            "\t\n" +
//            "SOU_SKILL\t\n" +
//            "\t\n" +
//            "SOU_SKILL_NAME\t\n" +
//            "\t\n" +
//            "SOU_TITLE_KEYWORD\t\n" +
//            "\t\n" +
//            "SOU_TITLE_NORMALIZED\t\n" +
//            "\t\n" +
//            "SOU_TITLE_SKILL\t\n" +
//            "\t\n" +
//            "SOU_BODY_KEYWORD\t\n" +
//            "\t\n" +
//            "SOU_BODY_SKILL\t\n" +
//            "\t\n" +
//            "SOU_NORMALIZED_SKILL";
    static String old = "\"similarJobLevelForCV_Exp5\"";
    static String separator = "\t\n\t\n";
    static String newValue = "\"similarJobLevelForCV_Exp5\"";

    public static void main(String[] args) throws IOException {

        List<String> projectNameList = FileUtils.readLines(new File(RPOJECT_NAME_FILE), Charset.defaultCharset());
        for (String s : projectNameList) {
            File repoDir = new File(String.format(gitFile, s));
            String inPathStr = String.format(inPath, s);
            //首先拉取远端代码
            GitUtils.gitPull(repoDir);

            File sFile = null;
            boolean flag = false;
            List<File> allFiles = FileUtil.getAllFiles(inPathStr);
            for (File singleFile : allFiles) {
                String oldValue = FileUtils.readFileToString(singleFile, Charset.defaultCharset());
//                if (isHaveTheBeforeValue(singleFile, old, separator, newValue))
                if (oldValue.contains(old))
                {
                    log.warn("需要修改的文件有：" + singleFile.getAbsolutePath());
                    String haveTheBeforeAllValueGetValue = isHaveTheBeforeAllValueGetValue(singleFile, old, separator);
                    FileUtils.write(new File(outputDir+"/"+projectName+"_need_update_file.txt"),singleFile.getAbsolutePath()+":"+ haveTheBeforeAllValueGetValue ,Charset.defaultCharset(),true);
                    FileUtils.write(new File(outputDir+"/"+projectName+"_need_update_file.txt"),"\r\n" ,Charset.defaultCharset(),true);
                    flag = true;
                    sFile = singleFile;
                }
            }

            if(flag){
                log.warn("需要修改的项目有：" + s);
                String haveTheBeforeValueGetValue = isHaveTheBeforeValueGetValue(sFile, old, separator, newValue);
                FileUtils.write(new File(outputDir+"/"+projectName+"_need_update_project.txt"),s+":"+haveTheBeforeValueGetValue ,Charset.defaultCharset(),true);
                FileUtils.write(new File(outputDir+"/"+projectName+"_need_update_project.txt"),"\r\n" ,Charset.defaultCharset(),true);
            }


        }

    }


    public static boolean isHaveTheBeforeValue(File file, String spiltBeforeValue, String separator, String newValue) throws IOException {

        String oldValue = FileUtils.readFileToString(file, Charset.defaultCharset());

        String replace = oldValue;

        String[] splitOld = spiltBeforeValue.split(separator);
        boolean flag = false;
        for (String s1 : splitOld) {
            String tmp = replace;
            replace = replace.replaceAll(s1, newValue);
            //此处进行判断哪些文本文件是修改了的
            if (replace.contains(newValue) && tmp.contains(s1) && !replace.equals(tmp)) {
                flag = true;
            }
        }
        return flag;
    }


    public static String isHaveTheBeforeValueGetValue(File file, String spiltBeforeValue, String separator, String newValue) throws IOException {

        String oldValue = FileUtils.readFileToString(file, Charset.defaultCharset());

        String returnValue = "";

        String replace = oldValue;

        String[] splitOld = spiltBeforeValue.split(separator);
        boolean flag = false;
        for (String s1 : splitOld) {
            String tmp = replace;
            replace = replace.replaceAll(s1, newValue);
            //此处进行判断哪些文本文件是修改了的
            if (replace.contains(newValue) && tmp.contains(s1) && !replace.equals(tmp)) {
                returnValue = s1;
                flag = true;
            }
        }
        if (flag){
            return returnValue;
        }else {
            return null;
        }
    }


    public static String isHaveTheBeforeAllValueGetValue(File file, String spiltBeforeValue, String separator) throws IOException {

        String oldValue = FileUtils.readFileToString(file, Charset.defaultCharset());

        String[] splitOld = spiltBeforeValue.split(separator);

        StringBuilder returnValue = new StringBuilder();

        boolean flag = false;
        for (String s1 : splitOld) {
            if (oldValue.contains(s1)) {
                returnValue.append(s1 + ",");
                flag = true;
            }
        }
        if (flag){
            return returnValue.toString();
        }else {
            return null;
        }
    }
}
